package com.ttn.util;

import java.util.Arrays;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

@Component
public class LanguageInterceptor extends HandlerInterceptorAdapter {
	
	@Autowired
	private LanguageBean languageBean;

	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		// TODO Auto-generated method stub
		List<String> pathSegments = Arrays.asList(request.getRequestURI().split("/"));
		if (!pathSegments.isEmpty()){	
			languageBean.setLocale(new Locale(pathSegments.get(2)));
		}
		return super.preHandle(request, response, handler);
	}

}
